package com.kiln.juxingqs.ui

import android.content.Intent
import android.view.View
import androidx.recyclerview.widget.DividerItemDecoration
import client.global.commlib.widget.EmptyLayout
import com.kiln.juxingqs.BaseMiddleActivity
import com.kiln.juxingqs.R
import com.kiln.juxingqs.adapter.ZrLotsKjyAdapter
import com.kiln.juxingqs.api.bean.GetDddBatchRet
import com.kiln.juxingqs.api.bean.GoodsDetail
import com.kiln.juxingqs.databinding.ActivityKjyLotsBinding
import com.kiln.juxingqs.ui.vm.ZrLotsViewModel

/**
 * 可交易商品
 */
class KjyLotsActivity : BaseMiddleActivity<ZrLotsViewModel, ActivityKjyLotsBinding>() {

    var goods_id: String? = null
    var goods_name: String? = null
    var goods_price: String? = null
    var f_price: String? = null
    var g_price: String? = null
    var num: String? = null

    override fun getLayout(): Int {
        return R.layout.activity_kjy_lots
    }

    val adapter: ZrLotsKjyAdapter by lazy {
        ZrLotsKjyAdapter(this)//1代表可交易
    }

    override fun initViewModel(): ZrLotsViewModel = createViewModel()

    override fun initView() {
        binding.event = this
        binding.vm = viewModel
        goods_id = intent.getStringExtra("goods_id")
        goods_name = intent.getStringExtra("goods_name")
        goods_price = intent.getStringExtra("goods_price")
        f_price = intent.getStringExtra("f_price")
        g_price = intent.getStringExtra("g_price")
        num = intent.getStringExtra("num")

        val decoration = DividerItemDecoration(this, DividerItemDecoration.VERTICAL)
        val dividerDrawable = resources.getDrawable(R.drawable.drawable_divider)
        decoration.setDrawable(dividerDrawable)
        binding.recyclerView.addItemDecoration(decoration)
        binding.recyclerView.adapter = adapter

        binding.refreshLayout.setOnRefreshListener {
            adapter.hasMoreState.value = true
            viewModel.pageOne.value = 0
            viewModel.holdSellBatchTrade(goods_id)
        }
        binding.recyclerView.onLoadMore = {
            if (adapter.hasMoreState.value!! && !viewModel.isOneLoading.value!!) {
                viewModel.holdSellBatchTrade(goods_id)
            }
        }
        binding.emptyLayout.setOnClickListener {
            viewModel.pageOne.value = 0
            viewModel.holdSellBatchTrade(goods_id)
        }
        viewModel.isOneLoading.observe(this, {
            if (!it) {
                binding.refreshLayout.isRefreshing = false
            }
        })

            binding.tvColumn2.text = "单价"
        binding.tvColumn3.text = "数量"
        binding.tvColumn4.visibility = View.GONE



    }

    override fun initData() {
        viewModel.pageOne.value = 0
        viewModel.holdSellBatchTrade(goods_id)
    }

    override fun observerUI() {
        viewModel.callOne.observe(this, {
            it?.let {
                if (it.isSuccess) {
                    val list =
                        it.data?.batch_list as MutableList<GetDddBatchRet.Batch?>?
                    if (viewModel.pageOne.value == 1) {
                        adapter.submitList(list)
                    } else
                        adapter.appendList(list)
                    if (list != null && list.size > 0) {
                        if (list.size < 20) {
                            adapter.hasMoreState.value = false
                        }
                        binding.emptyLayout.visibility = View.GONE
                    } else {
                        if (adapter.itemCount == 0)
                            binding.emptyLayout.errorType = EmptyLayout.NODATA
                        adapter.hasMoreState.value = false
                    }
                } else {
                    binding.emptyLayout.errorType = EmptyLayout.NETWORK_ERROR
                }
            }
        })
    }

//    //申请提货
//    fun applyDelivery() {
//        startActivity(Intent(this, ApplyDeliveryActivity::class.java))
//    }

    //商品一:20 商品二:21 特价券:22 可交易商品:23
    fun detail() {
        startActivity(Intent(this, XyzrNewActivity::class.java).apply {
            putExtra("detail_type", "23")
            putExtra("goods_id", goods_id)//新goods_id
        })
    }

    //挂单服务中心（协议转让）
    fun goGdServerCenter() {//num: String
        startActivity(
            Intent(this, ZrXyNewActivity::class.java).apply {
                putExtra("goods_id", goods_id)
                putExtra("goods_name", goods_name)
                putExtra("goods_price", goods_price)
                putExtra("f_price", f_price)
                putExtra("deal_num", num)
                putExtra("ZrXyType", "server")
                putExtra("serverType", 1)
                putExtra("serverName", "服务中心")
                putExtra("titleName", "挂单服务中心")
            })

    }


    //挂牌供应商（协议转让）
    fun goGdSpf() {
        startActivity(
            Intent(this, ZrXyNewActivity::class.java).apply {
                putExtra("goods_id", goods_id)
                putExtra("goods_name", goods_name)
                putExtra("goods_price", goods_price)
                putExtra("g_price", g_price)
                putExtra("deal_num", num)
                putExtra("ZrXyType", "server")
                putExtra("serverType", 2)
                putExtra("serverName", "供应商")
                putExtra("titleName", "挂牌供应商")
            })
    }

    //点对点交易（协议转让）
    fun goZrDddjy() {
        startActivity(Intent(this, ZrXyNewActivity::class.java).apply {
            putExtra("goods_id",goods_id)
            putExtra("goods_name", goods_name)
            putExtra("goods_price", goods_price)
            putExtra("deal_num", num)
            putExtra("titleName", "点对点交易")
        })
    }

    //商品购买
    fun lotBuy() {
//        val bannerList = intent.getParcelableArrayListExtra<GoodsDetail.Banner>("banner_list")
//        val detailList = intent.getParcelableArrayListExtra<GoodsDetail.Detail>("detail_list")
//        startActivity(Intent(this, LotDealActivity::class.java).apply {
//            putExtra("goods_id", goods_id)
//            putExtra("banner_list", bannerList)
//            putExtra("detail_list", detailList)
//
//        })
    }

}